package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Department;
import com.grom.po.GcmsUser;
import com.grom.po.RelationSecToDep;
import com.grom.po.Security;
import com.grom.service.DepartmentService;
import com.grom.service.GcmsUserService;
import com.grom.service.SecurityService;
import com.grom.util.JsonUtil;

public class DepartmentAction {
	public void gotoList() {
		List<Department> allDepartments = DepartmentService.instance
				.getAllDepartment();
		ContextHolder.setRequestAttribute("allDepartments", allDepartments);
		List<Security> securities = SecurityService.instance
				.getAllParentSecurity();
		Map<String, List<Security>> securityMaps = new HashMap<String, List<Security>>();
		for (Security security : securities) {
			List<Security> tmp = securityMaps.get(security.getParentID());
			if (tmp == null) {
				tmp = new ArrayList<Security>();
				securityMaps.put(security.getParentID(), tmp);
			}
			tmp.add(security);
		}
		ContextHolder.setRequestAttribute("securityMaps", securityMaps);
		ContextHolder.forward("/WEB-INF/jsp/department/update.jsp");
	}

	public void doSave() {
		Department department = (Department) ContextHolder
				.initFromTheRequest(Department.class);
		if (department.getID() == null || department.getID().trim().equals("")) {
			DepartmentService.instance.add(department);
		} else {
			DepartmentService.instance.update(department);
		}
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/DepartmentAction/gotoList.do");
	}

	public void doDelete() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("deleteId");
		System.out.println("| DepartmentAction | doDelete | id | " + id);
		Department department = new Department();
		department.setID(id);
		DepartmentService.instance.delete(department);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/DepartmentAction/gotoList.do");
	}

	public void conditionSelect() {
		String userName_s = ContextHolder.findInstance().getRequest()
				.getParameter("userName_s");
		String remark_s = ContextHolder.findInstance().getRequest()
				.getParameter("remark_s");
		String sql = "select * from department where username like '%"
				+ userName_s + "%' and remark like '%" + remark_s + "%'";
		List<Department> allDepartments = DepartmentService.instance
				.selectByCondition(sql);
		ContextHolder.setRequestAttribute("allDepartments", allDepartments);
		ContextHolder.forward("/WEB-INF/jsp/department/update.jsp");
	}

	public void getDetail() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		Department department = DepartmentService.instance
				.getDepartmentByID(id);
		String json = JsonUtil.parseToJson(department);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("department=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getBindSec() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		List<Security> securities = SecurityService.instance.getAllSecurityRelToDep(id);
		String json = JsonUtil.parseToJson(securities);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("securities=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getEmployee() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		List<GcmsUser> users = GcmsUserService.instance
				.getUsersByDepartment(id);
		String json = JsonUtil.parseToJson(users);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("users=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void doBind(){
		String securityId = ContextHolder.findInstance().getRequest()
				.getParameter("securityId");
		String departmentId = ContextHolder.findInstance().getRequest()
				.getParameter("departmentId");
		RelationSecToDep relation = new RelationSecToDep();
		relation.setDepartmentID(departmentId);
		relation.setSecurityID(securityId);
		DepartmentService.instance.addRelationToSec(relation);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/DepartmentAction/gotoList.do");
	}
	
	public void doRemove(){
		String securityId = ContextHolder.findInstance().getRequest()
				.getParameter("securityId");
		String departmentId = ContextHolder.findInstance().getRequest()
				.getParameter("departmentId");
		RelationSecToDep relation = new RelationSecToDep();
		relation.setDepartmentID(departmentId);
		relation.setSecurityID(securityId);
		DepartmentService.instance.removeRelationToSec(relation);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/DepartmentAction/gotoList.do");
	}
}
